package com.xdtech.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.sys.dao.ResourceDao;
import com.xdtech.sys.model.FilterGroup;
import com.xdtech.sys.model.FilterRule;
import com.xdtech.sys.model.FilterTranslator;
import com.xdtech.sys.model.Resource;
import com.xdtech.sys.service.FilterGroupService;
import com.xdtech.sys.service.FilterParamService;
import com.xdtech.sys.service.FilterRuleService;
import com.xdtech.sys.service.FilterTranslatorService;
import com.xdtech.sys.service.ResourceService;
import com.xdtech.sys.vo.ResourceItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-03-18 13:53:54
 * @since 1.0
 * @see
 */
@Service
public class ResourceServiceImpl implements ResourceService {
	@Autowired
	private ResourceDao resourceDao;
	@Autowired
	private FilterTranslatorService filterTranslatorService;
	@Autowired
	private FilterGroupService filterGroupService;
	@Autowired
	private FilterRuleService filterRuleService;
	@Autowired
	private FilterParamService filterParamService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param entity
	 */
	public void save(Resource entity) {
		resourceDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Resource> entities) {
		resourceDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param entity
	 */
	public void delete(Resource entity) {
		resourceDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		resourceDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param id
	 * @return
	 */
	public Resource get(Long id) {
		return resourceDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @return
	 */
	public List<Resource> getAll() {
		return resourceDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> resources = null;
		long rows = 0;
		if (pg!=null) {
			Page<Resource> page = resourceDao.findPage(pg,"from Resource order by createTime desc", values);
			resources = BeanUtils.copyListProperties(
					ResourceItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Resource> resourceList = resourceDao.find("from Resource order by id desc", values);
			resources = BeanUtils.copyListProperties(
					ResourceItem.class, resourceList);
			rows = resources.size();
		}
		results.put("total", rows);
		results.put("rows", resources);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateResource(ResourceItem item) {
		Resource resource = null;
		if (item.getId()==null) {
			resource = new Resource();
		}else {
			resource = resourceDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(resource, item);
		resourceDao.save(resource);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param resourceId
	 * @return
	 */
	public ResourceItem loadResourceItem(Long resourceId) {
		Resource resource = resourceDao.get(resourceId);
		ResourceItem resourceItem = new ResourceItem();
		BeanUtils.copyProperties(resourceItem, resource);
		return resourceItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteResourceInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-03-18 13:53:54
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteResourceInfo(List<Long> resourceIds) {
		for (Long id : resourceIds) {
			filterTranslatorService.deleteByResourceId(id);
			delete(id);
		}
		return true;
	}
	
	/* (非 Javadoc)
	 * updateResource
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param item
	 * @param ruleField
	 * @param ruleOption
	 * @param ruleRole
	 * @see com.xdtech.sys.service.ResourceService#updateResource(com.xdtech.sys.vo.ResourceItem, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void updateResource(ResourceItem item, String ruleField, String ruleType, String ruleValue,String ruleOption)
	{
		Resource resource = null;
		if (item.getId()==null) {
			resource = new Resource();
		}else {
			resource = resourceDao.get(item.getId());
		}
		BeanUtils.copyProperties(resource, item);
		resourceDao.save(resource);
		
		FilterTranslator translator = new FilterTranslator();
		translator.setResouceId(resource.getId());
		filterTranslatorService.save(translator);
		
		FilterGroup group = new FilterGroup();
		group.setTranslator(translator);
		group.setOption("and");
		filterGroupService.save(group);
		
		FilterRule rule = new FilterRule();
		rule.setField(ruleField);
		rule.setOption(ruleOption);
		rule.setType(ruleType);
		rule.setValue(ruleValue);
		rule.setFilterGroup(group);
		filterRuleService.save(rule);
		
		group.getFilterRules().add(rule);
		translator.getGroups().add(group);
		translator.createText();
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-3-21下午11:59:28
	 * @modified by
	 * @param resource
	 * @param translator
	 */
	@Override
	public void createResourcePermission(Resource resource,
			FilterTranslator translator) {
		if (resource.getId()!=null) {
			filterTranslatorService.deleteByResourceId(resource.getId());
		}
		save(resource);
		//保存过滤配置信息
		filterTranslatorService.saveTranslatorHasLinkInfo(resource.getId(),translator);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:25:17
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
